package com.redroid.findme;

import java.util.ArrayList;
import java.util.List;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class EdtSnAct extends ListActivity {

	DbHandler db = new DbHandler(this);
	List<Contact> clist;
	
	@Override
	protected void onCreate(Bundle edtAct) {
		super.onCreate(edtAct);
		
		clist = db.getAllContacts();
		ArrayList<String> users = new ArrayList<String>();
		
		
		for (Contact contact : clist) {
			String useraccount = contact.getUsername() + " (" + 
					contact.getSN() + ")";
				users.add(useraccount);
		}
		
		ArrayAdapter<String> menuadapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, users);
		setListAdapter(menuadapter);
	}
	
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		// TODO Auto-generated method stub
		super.onListItemClick(l, v, position, id);
		int tpos = clist.get(position).getID();
		String pos = Integer.toString(tpos);
		Toast.makeText(this, pos, Toast.LENGTH_SHORT).show();
		
		Intent editdetail = new Intent(EdtSnAct.this, DetailnEdit.class);
		Bundle edtbundle = new Bundle();
		edtbundle.putInt("contid", tpos);
		editdetail.putExtras(edtbundle);
		startActivity(editdetail);
		
	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		clist = db.getAllContacts();
		ArrayList<String> users = new ArrayList<String>();
		
		for (Contact contact : clist) {
			String useraccount = contact.getUsername() + " (" + 
					contact.getSN() + ")";
				users.add(useraccount);
		}
		
		ArrayAdapter<String> menuadapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, users);
		setListAdapter(menuadapter);
	}
	
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}
}
